package com.demo.result;

import com.alibaba.fastjson.JSONObject;
import com.taobao.pac.sdk.cp.dataobject.response.MDM_WENYAO_CP_INFO_QUERY.PageInfo;
import org.apache.commons.lang.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.demo.result.Return.*;

/**
 * @name: 李佳
 * @data: 2022/7/1 10:56
 * @className: ResultReturn
 * @author: LiJia
 **/
public class ResultReturn implements Serializable{
//    使用示例
//    public Result<返回实体类> 方法名(){
//        return ResultReturn<返回实体类>("key","value");
//    }

    /**
     *  返回成功带参数信息
     * @param <T> values
     * @return result
     */
    public static <T> Result<T> success(T values) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS.key);
        result.setMessage("成功");
        result.setData(values);
        return result;
    }


    /**
     * 兼容之前的ApiResultReturn
     * @data:2022/8/10
     * @param values
     * @param <T>
     * @return
     */
    public static <T> Result<T> success(String val, T values) {
        Result<T> result = new Result<>();
        result.setCode(SUCCESS.key);
        result.setMessage("成功");
        if(StringUtils.isNotEmpty(val)){
            HashMap<String,Object> hashMap=new HashMap();
            hashMap.put(val, values);
            result.setData((T) hashMap);
        }else{
            result.setData(values);
        }
        return result;
    }

    /**
     *  返回成功 无参数信息
     * @return null
     */
    public static <T> Result<T> success(){
        return new Result<>(SUCCESS.key,SUCCESS.value);
    }


    /**
     * token 失败 返回
     * @return token认证失败 返回 1000 重新登陆
     */
    public static <T> Result<T> successToken(){
        return new Result<>(SUCCESS_TOKEN.key,SUCCESS_TOKEN.value);
    }

    /**
     * 成功返回 201 状态 空值
     * @return 201
     */
    public static <T> Result<T> successNot(){
        return new Result<>(SUCCESS_NOT.key, SUCCESS_NOT.value);
    }

    /**
     * 错误
     * @param msg 错误信息
     * @return 返回错误信息
     */
    public static <T> Result<T> failError(String msg){
        return new Result<>(FAIL.key,msg);
    }

    /**
     * 签约失败返回
     * @param <T>
     * @return
     */
    public static <T> Result<T> failConfirm(String msg){
        return new Result<>(FAIL_CONFIRM.key,msg);
    }
    /**
     * 参数错误
     * @return 参数错误
     */
    public static <T> Result<T> failParam(){
        return new Result<>(PARAM_ERROR.key, PARAM_ERROR.value);
    }


    public static <T> Result<T> globalFail(){
        return new Result<>(GLOBAL_FAIL.key, GLOBAL_FAIL.value);
    }

    /**
     * 错误
     * @param msg 错误信息
     * @return
     * {
     *   "code": 900,
     *   "message": "msg",
     *   "data": null
     */
    public static JSONObject failMsg(String msg){
        JSONObject object = new JSONObject();
        object.put("code",900);
        object.put("message",msg);
        object.put("data",null);
        return object;
    }
}
